package yxy.game.pm2.active.module;

import com.alibaba.fastjson.JSONObject;
import org.joda.time.DateTime;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.charge;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PayInfoModule;
import yxy.game.pm2.bean.module.PlayerModule;

import java.util.HashMap;
import java.util.Map;

public class ActiveModule_元宝首冲 extends PlayerModule {

	//bean -------------------------------------------
	public static final int acttype = 0;
	
	private HashMap<Integer, DateTime> firstpaymap;	//首次充值时间
	
	public HashMap<Integer, DateTime> getFirstpaymap() {
		return firstpaymap;
	}
	public void setFirstpaymap(HashMap<Integer, DateTime> firstpaymap) {
		this.firstpaymap = firstpaymap;
	}
	
	//func --------------------------------------------
		
	public static ActiveModule_元宝首冲 create(String playerCode) {
		Map<String, charge> cmap = DataCenter.getAllData(charge.class);
		HashMap<Integer, DateTime> fpmap = new HashMap<Integer, DateTime>();
		DateTime dt0 = new DateTime(0);
		for (String key:cmap.keySet()) {
			charge c = cmap.get(key);
			if(c.getType() == PayInfoModule.普通充值) {
				fpmap.put(c.getId(), dt0);
			}
		}
		
		ActiveModule_元宝首冲 module = new ActiveModule_元宝首冲();
		module.setPlayerCode(playerCode);
		module.setFirstpaymap(fpmap);
		module.set();
		return module;
	}
	
	public JSONObject firstpayinfo() {
		JSONObject json = new JSONObject();
		HashMap<Integer, DateTime> fpmap = getFirstpaymap();
		for (int key:fpmap.keySet()) {
			DateTime dt = fpmap.get(key);
			boolean canfirst = dt.isEqual(new DateTime(0));
			json.put(String.valueOf(key), canfirst);
		}
		return json;
	}
	
	public static JSONObject firstpayinfo(String pcode) {
		JSONObject json = new JSONObject();
		ActiveModule_元宝首冲 module = get(pcode);
		HashMap<Integer, DateTime> fpmap = module.getFirstpaymap();
		for (int key:fpmap.keySet()) {
			DateTime dt = fpmap.get(key);
			boolean canfirst = dt.isEqual(new DateTime(0));
			json.put(String.valueOf(key), canfirst);
		}
		return json;
	}
	
	public void handlepay(int id) {
		charge c = DataCenter.getData(id, charge.class);
		if(c == null) {
			return;
		}
		
		if(c.getType() == PayInfoModule.普通充值) {
			if(!getFirstpaymap().containsKey(id)) {
				getFirstpaymap().put(id, DateTime.now());
				this.set();
			}else {
				DateTime time = getFirstpaymap().get(id);
				if(time.isEqual(new DateTime(0))) {
					getFirstpaymap().put(id, DateTime.now());
					this.set();
				}
			}
		}
	}
	
	//redis -------------------------------------------
	
	static private String key(String pcode) {
		return String.format("PLAYER:%s:ACTIVE:%S", pcode, acttype);
	}

	public void set() {
		try {
			String key1 = key(getPlayerCode());
			setv(key1, this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	static public ActiveModule_元宝首冲 get(String playerCode) {
		try {
			ActiveModule_元宝首冲 module = getv(key(playerCode));
			if (module == null) {
				module = MySQLCacheDAO.getV(key(playerCode), ActiveModule_元宝首冲.class);
				if(module != null){module.set();}//同步到redis

				if (module == null) {
					module = create(playerCode);
				}
			}

			return module;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	//存储到数据库的方法
	public static void saveToDB(String playerCode) {
		try {
			String key = key(playerCode);
			if (touchBucket(key).isExists()) {
				ActiveModule_元宝首冲 activeModule_元宝首冲 = getv(key(playerCode));
				MySQLCacheDAO.setV(key, activeModule_元宝首冲);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	//从redis移走数据的操作
	public static void removeAllFromRedis(String playerCode) {
		try {
			touchBucket(key(playerCode)).delete();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}
	// lock==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getPlayerCode()));
	}
}
